package common

import (
	"sync"
	"net/http"
	"github.com/tebeka/selenium"
)

var (
	cookies   []http.Cookie
	cookiesRW sync.RWMutex
)

func ConvertCookies(seleniumCookies []selenium.Cookie) []http.Cookie {
	mCookies := make([]http.Cookie, len(seleniumCookies))
	for i, cookie := range seleniumCookies {
		tempCookie := http.Cookie{
			Name:   cookie.Name,
			Value:  cookie.Value,
			Path:   cookie.Path,
			Domain: cookie.Domain,
			Secure: cookie.Secure,
		}
		mCookies[i] = tempCookie
	}
	return mCookies
}

func SetCookies(mCookies []http.Cookie) {
	cookiesRW.Lock()
	cookies = mCookies
	cookiesRW.Unlock()
}

func GetCookies() []http.Cookie {
	cookiesRW.RLock()
	defer cookiesRW.RUnlock()
	cloneCookie := *&cookies
	return cloneCookie
}
